<?php
/**********************************************************
Copyright (c) 2011 Wayne State University Library System
Licensed under the MIT License.
Redistributions of files must retain the above copyright notice.

This software is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

MapIt!
http://www.lib.wayne.edu/MapIt!/
http://code.google.com/XXXX
contact: Paul Gallagher, Developer Librarian WSULS
email: paul.gallagher@wayne.edu
***********************************************************/
require_once('config/config.inc.php');
require_once('../lib/functions.php');
require_once('MDB2.php');


//enable debug options
if ($debug == true) {
	ini_set('display_errors',1);
	error_reporting(E_ALL);	
}


//#####################  Page Logic  #############################
//sanitize the array
$clean = array();
foreach ($_GET as $key => $value) {
	$clean[$key] = mysql_real_escape_string($value);
}

// create MDB2 instance
$mdb2 = MDB2::factory($dsn);
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
$query = 'SELECT *, IF(ISNULL(location_subcode), location_code, CONCAT(location_code, location_subcode)) AS location_jointcode FROM locations WHERE IF(ISNULL(location_subcode), location_code, CONCAT(location_code, location_subcode)) = "' . $clean['location_code'] . '"';
$result = $mdb2->query($query);

$array = $result->fetchRow();
$result->free();
if ($debug == true) {
	print_r($array);
}

//redirect if a call number request
if ($array['content_type'] == 'call_number') {
		header( 'Location:callnumber.php?location_code=' . $array['location_jointcode']);  //no location, FAIL
}

//#####################  Form Submission Logic  #############################
//CALL RANGE UPDATE
//check for empty submission, prevents processing before form load
if (!empty($_POST) && $_POST[ID] == "overlayUpdate")		
	{
		 $mdb2 = MDB2::factory($dsn);
		 $query = "UPDATE locations SET
		 width = '".$_POST[width]."', 
		 height = '".$_POST[height]."', 
		 x = '".$_POST[x]."', 
		 y = '".$_POST[y]."'
		 WHERE location_code = '" . $clean['location_code'] . "'";
		 $affected =& $mdb2->exec($query);
			if (PEAR::isError($affected)) {
				die($affected->getMessage());
			}

		unlinkByWildcard('../maps/dynamic/');
}

//#####################  Display Code #############################
ob_start("callback");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WSULS Mapping System Update Form</title>
<script language="JavaScript" type="text/javascript" src="js/overlay.js"></script>
</head>
<body>
<?php include('menu.php'); ?>
<br />
<table width="100%" border="0" cellspacing="2" cellpadding="2" >
  <tr>
    <td colspan="2">
    <form enctype="multipart/form-data" action="<?php echo $PHP_SELF;?>" method="post" id="callUpdate" name="pointform">
        <table width="555" border="0" cellspacing="2" cellpadding="2">
          <tr>
            <td width="6%" bgcolor="#CCCCCC"><div align="center">Y</div></td>
            <td width="6%" bgcolor="#CCCCCC"><div align="center">X</div></td>
            <td width="13%" bgcolor="#CCCCCC"><div align="center">Width:</div></td>
            <td width="16%" colspan="2" bgcolor="#CCCCCC"><div align="center">Height:</div></td>
            <td width="6%"></td>
          </tr>
          <tr>
            <td bgcolor="#CCCCCC"><div align="center">
                <input name="y" type="text" style="width:30px" readonly />
              </div></td>
            <td bgcolor="#CCCCCC"><div align="center">
                <input name="x" type="text" style="width:30px" readonly />
              </div></td>
            <td bgcolor="#CCCCCC"><div align="center">
                <input name="width" type="text" style="width:30px" readonly />
              </div></td>
            <td bgcolor="#CCCCCC"><div align="center">
                <input name="height" type="text" style="width:30px" readonly />
              </div></td>
            <td><div align="center">
                <div align="center">
                  <input type="submit" value="Submit" />
                  <input type="hidden" name="ID" value="overlayUpdate" />
                </div>
              </div></td>
          </tr>
        </table>
      </form>
      </td>
  </tr>
</table>

      <div id="pointer_div" name="pointer_div" style="position:absolute;"></div>
      <img id="pointer" name="pointer" style="cursor: crosshair;" onClick="point_it(event)"  src="<?php echo('../maps/templates/' . $array['template']) ?>" alt="Please upload a map of <?php echo($array['template']) ?> to continue" />

						<div>Production Version:</div>
                        <iframe id="frame" width="100%" height="100%" name="frame" src="../index.php?location=<?php echo($array['location_code']) ?>" frameborder="0" scrolling="no">
                    <p>Your browser does not support iframes.</p>
                    </iframe> 

</body>
</html>
<?php ob_end_flush(); ?>